package com.javiercp.ayudaapp.mail;

import java.util.Date; 
import java.util.Properties; 
import javax.activation.CommandMap; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.activation.MailcapCommandMap; 
import javax.mail.BodyPart; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class MailManager extends javax.mail.Authenticator {

	private String muser; 
	private String mpass; 

	private String mhost; 

	private String mport; 
	private String msport;

	private boolean mauth; 

	private boolean mdebuggable; 

	public MailManager() { 
		mhost = "smtp.gmail.com";
		mport = "465";
		msport = "465";

		muser = "";
		mpass = "";
		
		mdebuggable = false; 		 
		mauth = true; 				

		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
		CommandMap.setDefaultCommandMap(mc); 
	}

	public MailManager(String user, String pass, String host, String port,
			String sport, boolean auth, boolean debuggable) {
		mhost = host;
		mport = port;
		msport = sport;
		
		muser = user;
		mpass = pass;
		
		mauth = auth;
		mdebuggable = debuggable;
		 
		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
		CommandMap.setDefaultCommandMap(mc); 
	} 

	public void send(MailMessage mailMessage) throws Exception { 
		
		if (mailMessage == null) {
			throw new IllegalArgumentException("message cannot be null.");
		}
		
		String[] to = mailMessage.getTo();
		String from = mailMessage.getFrom();
		String subject = mailMessage.getSubject();
		String body = mailMessage.getBody();
		
		if (!(to.length > 0) || from.equals("") || 
				subject.equals("") || body.equals("")) { 
			//PROBABLY SHOULD ALLOW EMPTY MESSAGES... 
			throw new IllegalArgumentException("Mail message not well formed.");
		}
		
		Multipart multipart = mailMessage.getMultipart();
		if (multipart == null) {
			multipart = new MimeMultipart(); 
			mailMessage.setMultipart(multipart);
		}
		
		
		Properties props = setProperties(); 

		Session session = Session.getInstance(props, this); 

		MimeMessage msg = new MimeMessage(session); 

		msg.setFrom(new InternetAddress(from)); 

		InternetAddress[] addressTo = new InternetAddress[to.length]; 
		int foo = to.length;
		for (int i = 0; i < foo; i++) { 
			addressTo[i] = new InternetAddress(to[i]); 
		} 
		msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 

		msg.setSubject(subject); 
		msg.setSentDate(new Date()); 

		BodyPart messageBodyPart = new MimeBodyPart(); 
		messageBodyPart.setText(body); 
		multipart.addBodyPart(messageBodyPart); 
 
		msg.setContent(multipart); 

		Transport.send(msg); 
	} 

	public void addAttachmentToMsg(MailMessage message, String filename) throws Exception { 

		if (message == null) {
			throw new IllegalArgumentException("message cannot be null.");
		}

		Multipart multipart = message.getMultipart();
		if (multipart == null) {
			multipart = new MimeMultipart(); 
			message.setMultipart(multipart);
		}

		BodyPart messageBodyPart = new MimeBodyPart(); 
		DataSource source = new FileDataSource(filename); 
		messageBodyPart.setDataHandler(new DataHandler(source)); 
		messageBodyPart.setFileName(filename); 

		multipart.addBodyPart(messageBodyPart); 
	} 
	
	@Override 
	public PasswordAuthentication getPasswordAuthentication() { 
		return new PasswordAuthentication(muser, mpass); 
	} 

	private Properties setProperties() { 
		Properties props = new Properties(); 

		props.put("mail.smtp.host", mhost); 

		if(mdebuggable) { 
			props.put("mail.debug", "true"); 
		} 

		if(mauth) { 
			props.put("mail.smtp.auth", "true"); 
		} 

		props.put("mail.smtp.port", mport); 
		props.put("mail.smtp.socketFactory.port", msport); 
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
		props.put("mail.smtp.socketFactory.fallback", "false"); 

		return props; 
	} 

	// Getters and setters ..........

	public String getUser() {
		return muser;
	}

	public void setUser(String user) {
		muser = user;
	}

	public String getPass() {
		return mpass;
	}

	public void setPass(String pass) {
		mpass = pass;
	}

	public String getHost() {
		return mhost;
	}

	public void setHost(String host) {
		mhost = host;
	}

	public String getPort() {
		return mport;
	}

	public void setPort(String port) {
		mport = port;
	}

	public String getSPort() {
		return msport;
	}

	public void setSPort(String sport) {
		msport = sport;
	}

	public boolean isAuth() {
		return mauth;
	}

	public void setAuth(boolean auth) {
		mauth = auth;
	}

	public boolean isDebuggable() {
		return mdebuggable;
	}

	public void setDebuggable(boolean debuggable) {
		mdebuggable = debuggable;
	}

}
